package gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;

/**
 * A form for a user to login to the system.
 * @author James Gunter -  jpgunter
 *
 */
public class LoginPanel extends JPanel implements ActionListener{
  public static int PREFFERED_HEIGHT = 400;
  public static int PREFFERED_WIDTH = 475;
  
  private JTextField my_email_text;
  private JButton my_login_button;
  private JLabel my_email_static_label;
  
  private MainWindow my_parent;
  
  /**
   * Create the panel.
   */
  public LoginPanel(MainWindow the_parent_window) {
    
    setPreferredSize(new Dimension(PREFFERED_WIDTH, PREFFERED_HEIGHT));
    
    my_parent = the_parent_window;
    
    SpringLayout springLayout = new SpringLayout();
    setLayout(springLayout);
    
    my_email_static_label = new JLabel("E-Mail:");
    springLayout.putConstraint(SpringLayout.NORTH, my_email_static_label, 140, SpringLayout.NORTH, this);
    springLayout.putConstraint(SpringLayout.WEST, my_email_static_label, 10, SpringLayout.WEST, this);
    add(my_email_static_label);
    
    my_email_text = new JTextField();
    my_email_text.setHorizontalAlignment(SwingConstants.TRAILING);
    springLayout.putConstraint(SpringLayout.NORTH, my_email_text, 140, SpringLayout.NORTH, this);
    springLayout.putConstraint(SpringLayout.WEST, my_email_text, 6, SpringLayout.EAST, my_email_static_label);
    springLayout.putConstraint(SpringLayout.EAST, my_email_text, 383, SpringLayout.EAST, my_email_static_label);
    add(my_email_text);
    my_email_text.setColumns(10);
    
    my_login_button = new JButton("Login");
    springLayout.putConstraint(SpringLayout.NORTH, my_login_button, 6, SpringLayout.SOUTH, my_email_text);
    springLayout.putConstraint(SpringLayout.EAST, my_login_button, 0, SpringLayout.EAST, my_email_text);
    my_login_button.addActionListener(this);
    add(my_login_button);

  }

  @Override
  public void actionPerformed(ActionEvent the_event) {
    if(the_event.getSource() == my_login_button){
      String e_mail = my_email_text.getText();
      my_parent.doLogin(e_mail);
      
    }
    
  }
}
